from core.resources import CustomerModelResource
from import_export.fields import Field
from apps.tms.models import JobLog
from apps.tms.job.utils.results import *


class JobLogResources(CustomerModelResource):
    status = Field(column_name='用例状态')
    analysis = Field(column_name='分析结论')
    step = Field(column_name='操作步骤')
    exp = Field(column_name='预期值')
    actual = Field(column_name='实际值')
    result = Field(column_name='执行结果')
    pcap = Field(column_name='样本数据')
    rule = Field(column_name='样本规则')
    binding = Field(column_name='绑定信息')

    class Meta:
        model = JobLog
        fields = ('number', 'name', 'level', 'precondition', 'status', 'analysis',
                  'remark', 'step', 'exp', 'actual', 'result', 'pcap', 'rule', 'binding')
        import_id_fields = ('number',)
        export_order = fields

    @staticmethod
    def dehydrate_status(instance: JobLog):
        return status_dict.get(instance.status) if (instance.status) else instance.status

    @staticmethod
    def dehydrate_analysis(instance: JobLog):
        return analysis_dict.get(instance.analysis) if (instance.analysis) else instance.analysis

    @staticmethod
    def dehydrate_step(instance: JobLog):
        content = ''
        for step in instance.steps:
            content = content + '%s.%s\n' % (step.get('id'), step.get('step'))
        return content

    @staticmethod
    def dehydrate_exp(instance: JobLog):
        content = ''
        for step in instance.steps:
            content = content + '%s.%s\n' % (step.get('id'), step.get('exp'))
        return content

    @staticmethod
    def dehydrate_actual(instance: JobLog):
        content = ''
        for step in instance.steps:
            content = content + \
                '%s.%s\n' % (step.get('id'), step.get('actual'))
        return content

    @staticmethod
    def dehydrate_result(instance: JobLog):
        content = ''
        for step in instance.steps:
            result = step.get('result')
            content = content + '%s.%s\n' % (step.get('id'), result_dict.get(result)) if (
                result) else (content + '%s.%s\n' % (step.get('id'), result))
        return content

    @staticmethod
    def dehydrate_pcap(instance: JobLog):
        content = ''
        for step in instance.steps:
            content = content + '%s.%s\n' % (step.get('id'), step.get('pcap'))
        return content

    @staticmethod
    def dehydrate_rule(instance: JobLog):
        content = ''
        for step in instance.steps:
            content = content + '%s.%s\n' % (step.get('id'), step.get('rule'))
        return content

    @staticmethod
    def dehydrate_binding(instance: JobLog):
        content = ''
        for step in instance.steps:
            if (step.get('binding')):
                if (instance.type == '11'):
                    binding = {
                        "产品名称": step.get('binding')[0]['product']['label'],
                        "报文名称": step.get('binding')[0]['packet']['label'],
                        "字段名称": step.get('binding')[0]['name']
                    }
                else:
                    binding = {
                        "产品名称": step.get('binding')[0]['product']['label'],
                        "协议名称": step.get('binding')[0]['protocol']['label'],
                        "字段名称": step.get('binding')[0]['name']
                    }
                content = content + '%s.%s\n' % (step.get('id'), binding)
        return content
